import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import iView from 'iview'
import store from '@/store'
import { setTitle,getUserInfo } from '@/libs/util'

Vue.use(Router)
const router = new Router({
  routes,
  mode: 'hash'
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  // 不可访问问价夹路径
  if (to.meta.folder) {
    next({ replace: true, name: 'error_404' })
  }
  // 验证权限
  if (to.meta.access) {
    getUserInfo()
      .then((res) => {
        if(!to.meta.access.includes(res.access)) {
          next({ replace: true, name: 'error_404' })
        }
      })
  }
  next()
})

router.afterEach(to => {
  setTitle(to, router.app)
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

export default router
